扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
本文主要向你演示如何使用JavaExcel API来读写Excel文件。关于JavaExcel API,这是一个开源的lib库。其相关的feature如下:
你可以在这里下载:http://jexcelapi.sourceforge.net/,然后,把jxl.jar加到你的Java的classpath中。
下面是两段例程,一段是如何创建Excel,一段是如何读取Excel。
创建Excel
001 |
package writer; |
002 |
|
003 |
import java.io.File; |
004 |
import java.io.IOException; |
005 |
import java.util.Locale; |
006 |
|
007 |
import jxl.CellView; |
008 |
import jxl.Workbook; |
009 |
import jxl.WorkbookSettings; |
010 |
import jxl.format.UnderlineStyle; |
011 |
import jxl.write.Formula; |
012 |
import jxl.write.Label; |
013 |
import jxl.write.Number; |
014 |
import jxl.write.WritableCellFormat; |
015 |
import jxl.write.WritableFont; |
016 |
import jxl.write.WritableSheet; |
017 |
import jxl.write.WritableWorkbook; |
018 |
import jxl.write.WriteException; |
019 |
import jxl.write.biff.RowsExceededException; |
020 |
|
021 |
public class WriteExcel { |
022 |
|
023 |
private WritableCellFormat timesBoldUnderline; |
024 |
private WritableCellFormat times; |
025 |
private String inputFile; |
026 |
|
027 |
public void setOutputFile(String inputFile) { |
028 |
this .inputFile = inputFile; |
029 |
} |
030 |
|
031 |
public void write() throws IOException, WriteException { |
032 |
File file = new File(inputFile); |
033 |
WorkbookSettings wbSettings = new WorkbookSettings(); |
034 |
|
035 |
wbSettings.setLocale( new Locale( "en" , "EN" )); |
036 |
|
037 |
WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); |
038 |
workbook.createSheet( "Report" , 0 ); |
039 |
WritableSheet excelSheet = workbook.getSheet( 0 ); |
040 |
createLabel(excelSheet); |
041 |
createContent(excelSheet); |
042 |
|
043 |
workbook.write(); |
044 |
workbook.close(); |
045 |
} |
046 |
|
047 |
private void createLabel(WritableSheet sheet) |
048 |
throws WriteException { |
049 |
// Lets create a times font |
050 |
WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10 ); |
051 |
// Define the cell format |
052 |
times = new WritableCellFormat(times10pt); |
053 |
// Lets automatically wrap the cells |
054 |
times.setWrap( true ); |
055 |
|
056 |
// Create create a bold font with unterlines |
057 |
WritableFont times10ptBoldUnderline = new WritableFont( |
058 |
WritableFont.TIMES, 10 , WritableFont.BOLD, false , |
059 |
UnderlineStyle.SINGLE); |
060 |
timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline); |
061 |
// Lets automatically wrap the cells |
062 |
timesBoldUnderline.setWrap( true ); |
063 |
|
064 |
CellView cv = new CellView(); |
065 |
cv.setFormat(times); |
066 |
cv.setFormat(timesBoldUnderline); |
067 |
cv.setAutosize( true ); |
068 |
|
069 |
// Write a few headers |
070 |
addCaption(sheet, 0 , 0 , "Header 1" ); |
071 |
addCaption(sheet, 1 , 0 , "This is another header" ); |
072 |
|
073 |
} |
074 |
|
075 |
private void createContent(WritableSheet sheet) throws WriteException, |
076 |
RowsExceededException { |
077 |
// Write a few number |
078 |
for ( int i = 1 ; i < 10 ; i++) { |
079 |
// First column |
080 |
addNumber(sheet, 0 , i, i + 10 ); |
081 |
// Second column |
082 |
addNumber(sheet, 1 , i, i * i); |
083 |
} |
084 |
// Lets calculate the sum of it |
085 |
StringBuffer buf = new StringBuffer(); |
086 |
buf.append( "SUM(A2:A10)" ); |
087 |
Formula f = new Formula( 0 , 10 , buf.toString()); |
088 |
sheet.addCell(f); |
089 |
buf = new StringBuffer(); |
090 |
buf.append( "SUM(B2:B10)" ); |
091 |
f = new Formula( 1 , 10 , buf.toString()); |
092 |
sheet.addCell(f); |
093 |
|
094 |
// Now a bit of text |
095 |
for ( int i = 12 ; i < 20 ; i++) { |
096 |
// First column |
097 |
addLabel(sheet, 0 , i, "Boring text " + i); |
098 |
// Second column |
099 |
addLabel(sheet, 1 , i, "Another text" ); |
100 |
} |
101 |
} |
102 |
|
103 |
private void addCaption(WritableSheet sheet, int column, int row, String s) |
104 |
throws RowsExceededException, WriteException { |
105 |
Label label; |
106 |
label = new Label(column, row, s, timesBoldUnderline); |
107 |
sheet.addCell(label); |
108 |
} |
109 |
|
110 |
private void addNumber(WritableSheet sheet, int column, int row, |
111 |
Integer integer) throws WriteException, RowsExceededException { |
112 |
Number number; |
113 |
number = new Number(column, row, integer, times); |
114 |
sheet.addCell(number); |
115 |
} |
116 |
|
117 |
private void addLabel(WritableSheet sheet, int column, int row, String s) |
118 |
throws WriteException, RowsExceededException { |
119 |
Label label; |
120 |
label = new Label(column, row, s, times); |
121 |
sheet.addCell(label); |
122 |
} |
123 |
|
124 |
public static void main(String[] args) throws WriteException, IOException { |
125 |
WriteExcel test = new WriteExcel(); |
126 |
test.setOutputFile( "c:/temp/lars.xls" ); |
127 |
test.write(); |
128 |
System.out |
129 |
.println( "Please check the result file under c:/temp/lars.xls " ); |
130 |
} |
131 |
} |
读取Excel
01 |
package reader; |
02 |
|
03 |
import java.io.File; |
04 |
import java.io.IOException; |
05 |
|
06 |
import jxl.Cell; |
07 |
import jxl.CellType; |
08 |
import jxl.Sheet; |
09 |
import jxl.Workbook; |
10 |
import jxl.read.biff.BiffException; |
11 |
|
12 |
public class ReadExcel { |
13 |
|
14 |
private String inputFile; |
15 |
|
16 |
public void setInputFile(String inputFile) { |
17 |
this .inputFile = inputFile; |
18 |
} |
19 |
|
20 |
public void read() throws IOException { |
21 |
File inputWorkbook = new File(inputFile); |
22 |
Workbook w; |
23 |
try { |
24 |
w = Workbook.getWorkbook(inputWorkbook); |
25 |
// Get the first sheet |
26 |
Sheet sheet = w.getSheet( 0 ); |
27 |
// Loop over first 10 column and lines |
28 |
|
29 |
for ( int j = 0 ; j < sheet.getColumns(); j++) { |
30 |
for ( int i = 0 ; i < sheet.getRows(); i++) { |
31 |
Cell cell = sheet.getCell(j, i); |
32 |
CellType type = cell.getType(); |
33 |
if (cell.getType() == CellType.LABEL) { |
34 |
System.out.println( "I got a label " |
35 |
+ cell.getContents()); |
36 |
} |
37 |
|
38 |
if (cell.getType() == CellType.NUMBER) { |
39 |
System.out.println( "I got a number " |
40 |
+ cell.getContents()); |
41 |
} |
42 |
|
43 |
} |
44 |
} |
45 |
} catch (BiffException e) { |
46 |
e.printStackTrace(); |
47 |
} |
48 |
} |
49 |
|
50 |
public static void main(String[] args) throws IOException { |
51 |
ReadExcel test = new ReadExcel(); |
52 |
test.setInputFile( "c:/temp/lars.xls" ); |
53 |
test.read(); |
54 |
} |
55 |
|
56 |
} |
濠电姷顣介埀顒€鍟块埀顒€缍婇幃妯诲緞閹邦剛鐣洪梺闈浥堥弲婊勬叏濠婂牊鍋ㄦい鏍ㄧ〒閹藉啴鏌熼悜鈺傛珚鐎规洘宀稿畷鍫曞煛閸屾粍娈搁梻浣筋嚃閸ㄤ即宕㈤弽顐ュС闁挎稑瀚崰鍡樸亜閵堝懎濮┑鈽嗗亝濠㈡ê螞濡ゅ懏鍋傛繛鍡樻尭鐎氬鏌嶈閸撶喎顕i渚婄矗濞撴埃鍋撻柣娑欐崌閺屾稑鈹戦崨顕呮▊缂備焦顨呴惌鍌炵嵁鎼淬劌鐒垫い鎺戝鐎氬銇勯弽銊ф噥缂佽妫濋弻鐔碱敇瑜嶉悘鑼磼鏉堛劎绠為柡灞芥喘閺佹劙宕熼鐘虫緰闂佽崵濮抽梽宥夊垂閽樺)锝夊礋椤栨稑娈滈梺纭呮硾椤洟鍩€椤掆偓閿曪妇妲愰弮鍫濈闁绘劕寮Δ鍛厸闁割偒鍋勯悘锕傛煕鐎n偆澧紒鍌涘笧閹瑰嫰鎼圭憴鍕靛晥闂備礁鎼€氱兘宕归柆宥呯;鐎广儱顦伴崕宥夋煕閺囥劌澧ù鐘趁湁闁挎繂妫楅埢鏇㈡煃瑜滈崜姘跺蓟閵娧勵偨闁绘劕顕埢鏇㈡倵閿濆倹娅囨い蹇涗憾閺屾洟宕遍鐔奉伓